<?php


namespace app\api\model;


use app\common\library\IDMan;
use app\common\model\BaseModel;
use common;
use ApiOper;

class BikeWhRecordModel extends BaseModel
{
    protected $name = 'bike_warehouse_record';

    function add_wh_in($info) {
        $paramlist = [
            'city_id' => 'string',
            'bike_id' => 'string',
            'imei' => 'string',
            'warehouse_id' => 'string',
            'warehouse_name' => 'string',
        ];
        $info = common::type_conver($info, $paramlist);

        $userinfo = ApiOper::instance()->userinfo();
        $info['id'] = IDMan::city_order_id($info['city_id']);
        $info['reason'] = BIKE_WH_RECORD_TYPE_NEW;
        $info['create_time'] = date('Y-m-d H:i:s');
        $info['user_id'] = $userinfo['id'];
        $info['user_name'] = $userinfo['name'];
        $info['user_phone'] = $userinfo['phone'];

        self::create($info);
    }

    function recall($info) {
        $paramlist = [
            'city_id' => 'string',
            'bike_id' => 'string',
            'imei' => 'string',
            'warehouse_id' => 'string',
            'warehouse_name' => 'string',
        ];

        $info = common::type_conver($info, $paramlist);

        $userinfo = ApiOper::instance()->userinfo();
        $info['id'] = IDMan::city_order_id($info['city_id']);
        $info['create_time'] = date('Y-m-d H:i:s');
        $info['user_id'] = $userinfo['id'];
        $info['user_name'] = $userinfo['name'];
        $info['user_phone'] = $userinfo['phone'];

        self::create($info);
    }

    function launch($info) {
        $paramlist = [
            'city_id' => 'string',
            'bike_id' => 'string',
            'imei' => 'string',
            'warehouse_id' => 'string',
            'warehouse_name' => 'string',
        ];

        $info = common::type_conver($info, $paramlist);
        $info['inv_state'] = BIKE_INV_STATE_IN_MARKET;

        $userinfo = ApiOper::instance()->userinfo();
        $info['id'] = IDMan::city_order_id($info['city_id']);
        $info['create_time'] = date('Y-m-d H:i:s');
        $info['user_id'] = $userinfo['id'];
        $info['user_name'] = $userinfo['name'];
        $info['user_phone'] = $userinfo['phone'];
        $info['channel'] = $userinfo['channel'];

        self::create($info);
    }

    function add($city_id, $type, $bike_id, $warehouse_id, $warehouse_name) {
        $bike_info = BikeModel::instance()->get_info_by_id($bike_id);
        $data = [
            'city_id' => $city_id,
            'bike_id' => $bike_id,
            'bike_sn' => $bike_info['sn'],
            'imei' => $bike_info['imei'],
            'warehouse_id' => $warehouse_id,
            'warehouse_name' => $warehouse_name,
        ];

        $userinfo = ApiOper::instance()->userinfo();
        $data['id'] = IDMan::city_order_id($data['city_id']);
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['user_id'] = $userinfo['id'];
        $data['user_name'] = $userinfo['name'];
        $data['user_phone'] = $userinfo['phone'];
        $data['channel'] = $userinfo['channel'];
        $data['type'] = $type;

        self::create($data);
    }

    function query_list($city_id, $bike_id, $pos, $limit) {
        $selectlist = [
            'id' => 'string',
            'imei' => 'string',
            'channel' => 'string',
            'type' => 'string',
            'user_phone' => 'string',
            'user_name' => 'string',
            'user_id' => 'string',
            'create_time' => 'string',
        ];

        $sfs = common::array_keys($selectlist);
        $st = "city_id = ? and bike_id = ?";

        $beans = [
            $city_id,
            $bike_id,
        ];


        //$st = $st . " limit $pos, $limit";

        $rows =$this->where($st,$beans)->field($sfs)->select();
        $l = [];
        foreach ($rows as $row) {
            $l[] = common::sql_row($selectlist, $row);
        }
        return $l;
    }
}